package com.talent.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.talent.common.PageConstants;
import com.talent.dao.CourseMapper;
import com.talent.dao.ScheduleMapper;
import com.talent.dao.TrainDetailMapper;
import com.talent.dao.TrainPlanMapper;
import com.talent.page.Page;
import com.talent.pojo.Schedule;
import com.talent.pojo.TrainDetail;
import com.talent.pojo.TrainPlan;
import com.talent.pojo.User;
import com.talent.service.TrainPlanService;
import com.talent.vo.TrainPlanVo;
@Service
public class TrainPlanServiceImpl implements TrainPlanService{
	@Autowired
	private TrainPlanMapper trainPlanMapper;
	@Autowired
	private TrainDetailMapper trainDetailMapper;
	@Autowired
	private ScheduleMapper scheduleMapper;
	@Autowired
	private CourseMapper courseMapper;
	@Override
	public Page<TrainPlanVo> selectTrainPlanPage(TrainPlanVo trainPlan,User user, int currentPage) {
		Page<TrainPlanVo> page = new Page<>(currentPage, PageConstants.PAGESIZE);
        List<TrainPlanVo> trainPlanList = trainPlanMapper.selectTrainPlanPage(trainPlan,user, page);
        page.setResultList(trainPlanList);
        return page;
	}
	@Override
	public void deleteAll(String[] ids) {
		trainPlanMapper.deleteAll(ids);
		trainDetailMapper.deleteAll(ids);
	}
	@Override
	public TrainPlanVo selectVoByPrimaryKey(Integer id) {
		return trainPlanMapper.selectVoByPrimaryKey(id);
	}
	@Override
	public void updateStatus(Integer id, int sta) {
		trainPlanMapper.updateStatus(id,sta);
	}
	@Override
	public void updateSuggestion(int id, String suggestion) {
		trainPlanMapper.updateSuggestion(id,suggestion);
	}
	@Override
	public void add(Integer enterterm,String score1, String trainPlanName, Integer builderId, String status, String term,
			String ability, String goal, Integer majorId) {
		TrainPlan trainPlan = new TrainPlan();
		trainPlan.setEnterterm(enterterm);
		trainPlan.setName(trainPlanName);
		trainPlan.setBuilderId(builderId);
		trainPlan.setScore(score1);
		trainPlan.setStatus(status);
		trainPlan.setTerm(term);
		trainPlan.setAbility(ability);
		trainPlan.setGoal(goal);
		trainPlan.setMajorId(majorId);
		trainPlanMapper.insert(trainPlan);
	}
	@Override
	public int selectLatestTPId() {
		return trainPlanMapper.selectLatestTPId();
	}
	@Override
	public void deleteById(Integer id) {
		trainPlanMapper.deleteByPrimaryKey(id);
	}
	@Override
	public void edit(Integer trainPlanId, String term, String goal, String ability, String score1, String[] courseName,
			String[] courseCode, Double[] score, Double[] theory, Double[] experiment, Double[] selfmaster,
			String[] examtype, Integer[] termth, Integer[] scheduleId) {
		TrainPlan trainPlan = trainPlanMapper.selectByPrimaryKey(trainPlanId);
		trainPlan.setTerm(term);
		trainPlan.setGoal(goal);
		trainPlan.setAbility(ability);
		trainPlan.setSuggestion(null);
		trainPlan.setStatus("0");
		trainPlan.setScore(score1);
		trainPlanMapper.updateByPrimaryKeySelective(trainPlan);
		for(int i = 0; i<scheduleId.length; i++){
			Schedule schedule = scheduleMapper.selectByPrimaryKey(scheduleId[i]);
			Integer courseId = courseMapper.selectIdByCourseName(courseName[i]);
			schedule.setCourseId(courseId);
			schedule.setScore(score[i]);
			schedule.setTheory(theory[i]);
			schedule.setExperiment(experiment[i]);
			schedule.setSelfmaster(selfmaster[i]);
			schedule.setExamtype(examtype[i]);
			schedule.setTerm(termth[i]);
			scheduleMapper.updateByPrimaryKeySelective(schedule);
		}
		if(scheduleId.length<courseCode.length){
			for(int j=scheduleId.length;j<courseCode.length;j++){
				Integer courseId = courseMapper.selectIdByCourseName(courseName[j]);
				Schedule schedule =  new Schedule();
				schedule.setCourseId(courseId);
				schedule.setScore(score[j]);
				schedule.setTheory(theory[j]);
				schedule.setExperiment(experiment[j]);
				schedule.setSelfmaster(selfmaster[j]);
				schedule.setExamtype(examtype[j]);
				schedule.setTerm(termth[j]);
				scheduleMapper.insertSelective(schedule);
				Integer scheduleId1 = scheduleMapper.selectTop();
				TrainDetail trainDetail = new TrainDetail();
				trainDetail.setScheduleId(scheduleId1);
				trainDetail.setTrainplanId(trainPlanId);
				trainDetailMapper.insert(trainDetail);
			}
		}
	}
	@Override
	public int selectCount(String trainPlanName) {
		return trainPlanMapper.selectCount(trainPlanName);
	}

}
